﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Drawing;

namespace Line64.ImageFlow.Activities
{

    public sealed class AdjustBrightnessActivity : CodeActivity
    {

        public InOutArgument<Bitmap> Bitmap { get; set; }

        public InArgument<int> Brightness { get; set; }

        protected override void Execute(CodeActivityContext context)
        {

            Bitmap bitmap = this.Bitmap.Get(context);
            int brightness = this.Brightness.Get(context);

            for (int y = 0; y < bitmap.Height; y++)
            {
                
                for (int x = 0; x < bitmap.Width; x++)
                {
                    
                    Color oldColor = bitmap.GetPixel(x, y);

                    int red = oldColor.R + brightness;
                    red = Math.Min(red, 255);

                    int green = oldColor.G + brightness;
                    green = Math.Min(green, 255);

                    int blue = oldColor.B + brightness;
                    blue = Math.Min(blue, 255);

                    Color newColor = Color.FromArgb(red, green, blue);

                    bitmap.SetPixel(x, y, newColor);

                }

            }

            this.Bitmap.Set(context, bitmap);

        }

    }

}